package com.example.primomobile;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.view.DragEvent;
import android.view.View;
import android.view.View.OnDragListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.primomobile.utils.Utils;

class PecasAddFuncaoDragListener implements OnDragListener {
	private int countQtdPecasFunction;
	private Context context;

	public PecasAddFuncaoDragListener(  Context context, int countQtdPecasFunction) {
		super();
		this.countQtdPecasFunction = countQtdPecasFunction;
		this.context = context;
	}

	@Override
	public boolean onDrag( View v, DragEvent event ) {

		HashMap<String, Object> hashMap;
		switch ( event.getAction() ) {
			case DragEvent.ACTION_DRAG_STARTED:

				break;
			case DragEvent.ACTION_DRAG_ENTERED:
				break;
			case DragEvent.ACTION_DRAG_EXITED:
				break;
			case DragEvent.ACTION_DROP:

				ImageView view = (ImageView) event.getLocalState();

				hashMap = ( (HashMap<String, Object>) view.getTag() );

				if ( view.getTag() != null && !( (Boolean) hashMap.get( Utils.REMOVE ) ) ) {
					ImageView clone = new ImageView( context );
					LinearLayout linearLayout = (LinearLayout) v;

					Map<String, Object> map = new HashMap<String, Object>();
					map.put( Utils.REMOVE, true );
					map.put( Utils.PECA, hashMap.get( Utils.PECA ) );
					map.put( Utils.POSICAO, 1 );

					if ( ( (String) hashMap.get( Utils.PECA ) ).equals( Utils.FRENTE ) ) {
						clone.setImageResource( R.drawable.frente1 );
					} else if ( ( (String) hashMap.get( Utils.PECA ) ).equals( Utils.ESQUERDA ) ) {
						clone.setImageResource( R.drawable.esquerda1 );
					} else if ( ( (String) hashMap.get( Utils.PECA ) ).equals( Utils.DIREITA ) ) {
						clone.setImageResource( R.drawable.direita1 );
					} else if ( ( (String) hashMap.get( Utils.PECA ) ).equals( Utils.FUNCAO ) ) {
						clone.setImageResource( R.drawable.funcao1 );
					}

					clone.setOnTouchListener( null );
					clone.setOnLongClickListener( new PecasLongClickListener() );
					clone.setTag( map );

					clone.setLayoutParams( new GridView.LayoutParams( 120, 120 ) );
					clone.setScaleType( ImageView.ScaleType.CENTER_CROP );
					clone.setPadding( 0, 4, 0, 4 );

					linearLayout.removeViewAt( countQtdPecasFunction );
					linearLayout.addView( clone, countQtdPecasFunction );
					countQtdPecasFunction++;

					clone.setVisibility( View.VISIBLE );
				} else {
					view.setVisibility( View.VISIBLE );
				}

				break;
			case DragEvent.ACTION_DRAG_ENDED:
			default:
				break;
		}
		return true;
	}
}
